package com.hqyj.project.utils;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;

public class ClockInUtil {
    public static String[] clockIn() {


        String[] record = new String[13];
        // 上午上班时间
        LocalTime morningStartTime = LocalTime.of(8, 30);
        // 上午下班时间
        LocalTime morningEndTime = LocalTime.of(12, 0);
        // 下午上班时间
        LocalTime afterStartTime = LocalTime.of(12, 31);
        // 下午下班时间
        LocalTime afterEndTime = LocalTime.of(17, 30);

        //上午上班打卡时间范围
        LocalTime morningSignInClockInStartTime = morningStartTime.minus(30, ChronoUnit.MINUTES);
        LocalTime morningSignInClockInEndTime = morningStartTime.plus(30, ChronoUnit.MINUTES);
        //上午下班打卡时间范围
        LocalTime morningSignOutClockInStartTime = morningEndTime.minus(30, ChronoUnit.MINUTES);
        LocalTime morningSignOutClockInEndTime = morningEndTime.plus(30, ChronoUnit.MINUTES);
        //下午上班打卡时间范围
        LocalTime afterSignInClockInStartTime = afterStartTime.minus(30, ChronoUnit.MINUTES);
        LocalTime afterSignInClockInEndTime = afterStartTime.plus(30, ChronoUnit.MINUTES);
        //下午下班打卡时间范围
        LocalTime afterSignOutClockInStartTime = afterEndTime.minus(30, ChronoUnit.MINUTES);
        LocalTime afterSignOutClockInEndTime = afterEndTime.plus(30, ChronoUnit.MINUTES);

        // 获取当前时间
        LocalTime currentTime = LocalTime.now();

        LocalDateTime now = LocalDateTime.now();

        // 日期格式
        DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        // 时间格式
        DateTimeFormatter timeFormat = DateTimeFormatter.ofPattern("HH:mm:ss");
        // 只包含年月的日期格式
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM");
        // 格式化当前日期
        String nowDate = now.format(dateFormat);
        // 格式化当前时间
        String nowTime = now.format(timeFormat);

        String settingDate = now.format(dateTimeFormatter);

        String[] split = nowDate.split("-");
        // 年
        String nowYear = split[0];
        // 月
        String nowMonth = split[1];
        // 日
        String nowDay = split[2];
        // 打卡情况
        String attendanceRecord = "";
        // 备注
        String attendanceRemark = "";
        // 打卡状况（正常、迟到、早退）
        String mornSignIn = "";
        String mornSignOut = "";
        String afterSignIn = "";
        String afterSignOut = "";
        String clockRemark = "";
        if (currentTime.isAfter(morningSignInClockInStartTime) && currentTime.isBefore(morningSignInClockInEndTime)) {
            attendanceRecord = "上午签到";

            if (currentTime.isAfter(morningStartTime)) {
                attendanceRemark = "迟到";
            } else {
                attendanceRemark = "正常打卡";
            }
            mornSignIn = attendanceRemark;
        } else if (currentTime.isAfter(morningSignOutClockInStartTime) && currentTime.isBefore(morningSignOutClockInEndTime)) {
            attendanceRecord = "上午签退";
            if (currentTime.isBefore(morningEndTime)) {
                attendanceRemark = "早退";
            } else {
                attendanceRemark = "正常打卡";
            }
            mornSignOut = attendanceRemark;
        } else if (currentTime.isAfter(afterSignInClockInStartTime) && currentTime.isBefore(afterSignInClockInEndTime)) {
            attendanceRecord = "下午签到";
            if (currentTime.isAfter(afterStartTime)) {
                attendanceRemark = "迟到";
            } else {
                attendanceRemark = "正常打卡";
            }
            afterSignIn = attendanceRemark;
        } else if (currentTime.isAfter(afterSignOutClockInStartTime) && currentTime.isBefore(afterSignOutClockInEndTime)) {
            attendanceRecord = "下午签退";
            if (currentTime.isBefore(afterEndTime)) {
                attendanceRemark = "早退";
            } else {
                attendanceRemark = "正常打卡";
            }
            afterSignOut = attendanceRemark;
        } else {
            attendanceRecord = "当前不在上班打卡时间范围内";
            clockRemark = attendanceRecord;
        }
        record[0] = nowDate;
        record[1] = nowTime;
        record[2] = attendanceRecord;
        record[3] = attendanceRemark;
        record[4] = nowYear;
        record[5] = nowMonth;
        record[6] = nowDay;
        record[7] = mornSignIn;
        record[8] = mornSignOut;
        record[9] = afterSignIn;
        record[10] = afterSignOut;
        record[11] = clockRemark; // 备注
        record[12] = settingDate;  // xx年xx月
        System.out.println(Arrays.toString(record));
        return record;
    }

//    public static void main(String[] args) {
//        clockIn();
//    }
}
